package com.nf.handlers;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.nf.MiniResultSetHandler;

/**
 * 　　* @description: TODO
 * 　　* @param
 * 　　* @return
 * 　　* @throws
 * 　　* @author Administrator
 * 　　* @date $ $
 *
 */
public class MiniScalarHandler<T> implements MiniResultSetHandler<T> {
    private String columnName = null;
    private int columnIndex = 1;

    public MiniScalarHandler() {
        this(1,null);
    }

    public MiniScalarHandler(int columnIndex) {
        this(columnIndex,null);
    }

    public MiniScalarHandler(String columnName) {
        this(-1,columnName);
    }

    public MiniScalarHandler(int columnIndex,String columnName) {
        this.columnIndex = columnIndex;
        this.columnName = columnName;
    }


    @Override
    public T handle(ResultSet rs) throws SQLException {
        if (rs.next()) {
            if (columnName != null) {
                return (T) rs.getObject(columnName);
            }
            return (T) rs.getObject(columnIndex);
        }
        return null;
    }
}
